import { Component, OnInit,Pipe, PipeTransform } from '@angular/core';
import {DomSanitizer} from "@angular/platform-browser"; 
import {HttpClient,HttpHeaders} from "@angular/common/http";
import {environment} from 'src/environments/environment';
import { Router,ActivatedRoute,NavigationExtras } from '@angular/router';
import { DefaultLanguageService } from '@services/default-language.service';
import { TranslateService } from '@ngx-translate/core';
import { CommonResponse,VerificationType,ToastColor,CommonService } from '@services/common-service.service';
import { NavController  } from '@ionic/angular';

@Pipe({  
  name: 'html'  
})  
export class HtmlPipe implements PipeTransform {  
  
  constructor(
    private sanitizer: DomSanitizer) {  
  }  
  
  transform(html) {  
    return this.sanitizer.bypassSecurityTrustHtml(html);  
  }  
}

@Component({
  selector: 'app-comm-agreement',
  templateUrl: './comm-agreement.page.html',
  styleUrls: ['./comm-agreement.page.scss'],
})
export class CommAgreementPage implements OnInit {
  public readSeconds:number;

  public btnDisabled:boolean = true;

  public appUserId:string;

  public agreementTitle:string;

  public content:string;

  public showContent:boolean = false;

  //only look
  public onlyLook:boolean = true;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    public http:HttpClient,
    public navController: NavController,
    private defaultLanguageService:DefaultLanguageService,
    private translate:TranslateService
  ) {
    this.initLanguage();
   }

  ngOnInit() {
    this.getAgreement();
  }

  
  backToPrevPage(){
    this.navController.back();
  }

  initLanguage(){
    let defaultLanguage = this.defaultLanguageService.getDefaultLanguage();
    this.translate.setDefaultLang(defaultLanguage);
    this.translate.use(defaultLanguage); 
  }

  getAgreement(){
    var api = "/scm/app/getAppAgreement.do";
    var requestJson = {
      'agreementCode':environment.AGREEMENT_CODE,
      'version':environment.AGREEMENT_VERSION,
      'appCode':environment.APP_CODE,
      'language':this.defaultLanguageService.getDefaultLanguage()
    }
    this.http.post(api,requestJson).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        this.content = response.responseBody.content;
        this.agreementTitle = response.responseBody.agreementTitle;
        this.readSeconds = response.responseBody.readSeconds;
        this.showContent = true;
        var id = setInterval(() => {
          this.readSeconds--;
          if(this.readSeconds == 0){
            clearInterval(id);
            this.btnDisabled = false;
          }
        }, 1000);
      }
    },
    (errorResp)=>{
      console.log(errorResp);
    });
  }
}
